/**
 * 首页相关功能使用脚本
 */

    //region 添加格式化时间的方法
Date.prototype.formatDate = function (format) {
    var date = this;
    format = format || "yyyy-MM-dd";
    var dict = {
        "yyyy": date.getFullYear(),
        "M": date.getMonth() + 1,
        "d": date.getDate(),
        "H": date.getHours(),
        "m": date.getMinutes(),
        "s": date.getSeconds(),
        "MM": ("" + (date.getMonth() + 101)).substr(1),
        "dd": ("" + (date.getDate() + 100)).substr(1),
        "HH": ("" + (date.getHours() + 100)).substr(1),
        "mm": ("" + (date.getMinutes() + 100)).substr(1),
        "ss": ("" + (date.getSeconds() + 100)).substr(1)
    };
    return format.replace(/(yyyy|MM?|dd?|HH?|ss?|mm?)/g, function () {
        return dict[arguments[0]];
    });
}
//endregion

$(function () {
    var $right = $("#index-right");
    if ($right.length > 0) {
        var tarheight = $(window).height() - 100;
        var tarwidth = ($(window).width() - 241);
        $right.height(tarheight);
        $right.width(tarwidth);
        $(window).resize(function () {
            var tarheight = $(window).height() - 100;
            var tarwidth = ($(window).width() - 241);
            $right.height(tarheight);
            $right.width(tarwidth);
        });
    }
    var $time = $("#index-time");
    if ($time.length > 0) {
        var $area = $("#index-time-area");
        var date = new Date();
        if (date.getHours() > 18) {
            $area.html("晚上");
        } else if (date.getHours() < 8) {
            $area.html("早上");
        } else if (date.getHours() < 12) {
            $area.html("上午");
        } else {
            $area.html("下午");
        }
        $time.html(date.formatDate("yyyy-MM-dd HH:mm"));
        setInterval(function () {
            var date = new Date();
            if (date.getHours() > 18) {
                $area.html("晚上");
            } else if (date.getHours() < 8) {
                $area.html("早上");
            } else if (date.getHours() < 12) {
                $area.html("上午");
            } else {
                $area.html("下午");
            }
            $time.html(date.formatDate("yyyy-MM-dd HH:mm"));
        }, 10000);
    }
    var group = $("#index-head-menu").children();//顶部菜单
    var menu = $("#index-menu-left").children();//左边菜单
    //绑定顶部菜单事件并产生菜单
    group.each(function (index) {
        $(this).click(function () {
            group.removeClass("index-head-menu-active");
            $(this).addClass("index-head-menu-active");
            menu.hide().eq(index).show();
        });
    }).eq(0).addClass("index-head-menu-active");
    menu.each(function () {
        var sub = $(this).children();
        sub.each(function (index) {
            $(this).children("span").click(function () {
                sub.removeClass("index-menu-sub-active").eq(index).addClass("index-menu-sub-active");
            });
        }).eq(0).addClass("index-menu-sub-active");
    }).eq(0).show();
    var $first = $("#index-par").children();
    var $second = $("#index-sub-group").children();
    if ($first.length) {
        $first.each(function (i) {
            $(this).click(function () {
                $second.removeClass("active").eq(i).addClass("active");
            });
        }).eq(0).addClass("active");
    }
    if ($second.length) {
        $second.each(function () {
            var $third = $(this).children("ul").children();
            $third.click(function () {
                $third.removeClass("active");
                $(this).addClass("active");
            });
            $third.eq(0).addClass("active");
            $third.children("span").click(function () {
                $("#index-right").attr("src", $(this).attr("data-url"));
            });
        });
    }
});